#include "main.h"
#include "scnsInclude.h"
#include "stm32g4xx_it.h"

extern void test(void);

static void deviceCheck(void)
{
#if 0
#define check(x)    ((RCC->RSR&(x))==(x))
    if(check(RCC_RSR_PORRSTF|RCC_RSR_PINRSTF|RCC_RSR_BORRSTF|RCC_RSR_D2RSTF|RCC_RSR_D1RSTF|RCC_RSR_CPURSTF))
    {
        RAM_BACKUP_RESET_INFO[0]=RAM_BACKUP_RESET_INFO[1]=RAM_BACKUP_RESET_INFO[2]=0;
        printfFL("Reset by pwr_por_rst\n");
    }
    else if(check(RCC_RSR_SFTRSTF|RCC_RSR_PINRSTF|RCC_RSR_CPURSTF))
        printfFL("Reset by SFTREST\n");
    else if(check(RCC_RSR_IWDG1RSTF|RCC_RSR_PINRSTF|RCC_RSR_CPURSTF))
        printfFL("Reset by IWDG1\n");
    else if(check(RCC_RSR_PINRSTF|RCC_RSR_CPURSTF))
        printfFL("Reset by NRST\n");
    else if(check(RCC_RSR_CPURSTF))
        printfFL("Reset by CPUREST\n");
    else
        printfFL("Reset by 0X%08X\n",RCC->RSR);
#undef check
    if(RAM_BACKUP_RESET_INFO[0]||RAM_BACKUP_RESET_INFO[1]||RAM_BACKUP_RESET_INFO[2])
    {
        const char*file=((const char*)RAM_BACKUP_RESET_INFO[0]?(const char*)RAM_BACKUP_RESET_INFO[0]:"");
        const char*code=((const char*)RAM_BACKUP_RESET_INFO[2]?(const char*)RAM_BACKUP_RESET_INFO[2]:"");
        RAM_BACKUP_RESET_INFO[0]=RAM_BACKUP_RESET_INFO[1]=RAM_BACKUP_RESET_INFO[2]=0;
        printfFL("Last reset at file %s line %d code %s\n",file,RAM_BACKUP_RESET_INFO[1],code);
    }
    RCC->RSR|=RCC_RSR_RMVF;
#endif
}

void init(void)
{
    uint16 errN=0;
    uint64 displayUs=0;
    static const Point errPlu={0,0};
    scnsTryInit(SCNS_STATUS_OK,scnsYingInit());
    scnsTryInit(SCNS_STATUS_OK,scnsTimerInit());
    //scnsTryInit(SCNS_STATUS_OK,scnsLedInit(SCNS_LED_0));
    //scnsTryInit(SCNS_STATUS_OK,scnsLedInit(SCNS_LED_1));
    //scnsTryInit(SCNS_STATUS_OK,scnsIwdgInit());
    //scnsTryInit(SCNS_STATUS_OK,scnsBeepInit());
    //HAL_TIM_Base_Start_IT(&htim6);
    //HAL_TIM_Base_Start_IT(&htim7);
    //scnsBeepAdd("1");
    
    //scnsTryInit(SCNS_STATUS_OK,scnsKeyInit());
    //scnsTryInit(SCNS_STATUS_OK,scnsSmctpInit());
    //scnsTryInit(SCNS_STATUS_OK,scnsW25qxxInit(SCNS_W25QXX_SPI));
    //scnsTryInit(SCNS_STATUS_OK,scnsMenuInit());
    //scnsTryInit(SCNS_STATUS_OK,scnsLcdInit());
    //scnsTryInit(SCNS_STATUS_OK,scnsKeyboardGpioInit());
    //scnsTryInit(SCNS_STATUS_OK,scnsW25qxxTest(SCNS_W25QXX_SPI,0*4096,0));
    //deviceCheck();
    //scnsMenuAddTest(9);
    
    //scnsTryInit(SCNS_STATUS_OK,scnsIicInit(SCNS_IIC_SOFT_0));
    //scnsTryInit(SCNS_STATUS_OK,scnsIicInit(SCNS_IIC_SOFT_1));
    //scnsTryInit(SCNS_STATUS_OK,scnsAt24c02Init(SCNS_AT24C02_1));
    //scnsTryInit(SCNS_STATUS_OK,scnsAt24c02Test(SCNS_AT24C02_1,0));
    //scnsIicScan(SCNS_IIC_SOFT_1);
    //scnsIicScan(SCNS_IIC_SOFT_0);
    //scnsTryInit(SCNS_STATUS_OK,scnsTouchScreenInit());
#if defined(SCNS_ST7796_ENABLE)&&SCNS_ST7796_ENABLE==1&&defined(SCNS_GT911_ENABLE)&&SCNS_GT911_ENABLE==1&&defined(SCNS_TOUCH_SCREEN_ENABLE)&&SCNS_TOUCH_SCREEN_ENABLE==1
    for(int i=0;i<(sizeof mainPageButtons)/(sizeof(mainPageButtons[0]));++i)
        scnsTouchScreenAddButton(&mainPageButtons[i]);
#endif
    //scnsBeepAdd("2");
#if defined(SCNS_DS18B20_ENABLE)&&SCNS_DS18B20_ENABLE==1
    scnsTryInit(SCNS_STATUS_OK,scnsDs18b20Init(SCNS_DS18B20_1));
#endif
#if defined(SCNS_DHT11_ENABLE)&&SCNS_DHT11_ENABLE==1
    scnsTryInit(SCNS_STATUS_OK,scnsDht11Init(SCNS_DHT11_1));
#endif
#if defined(SCNS_NRF24L01_ENABLE)&&SCNS_NRF24L01_ENABLE==1
    scnsTryInit(SCNS_STATUS_OK,scnsNrf24l01Init(SCNS_NRF24L01_1));
#endif
#if defined(SCNS_GYRO_ENABLE)&&SCNS_GYRO_ENABLE==1
    scnsTryInit(SCNS_STATUS_OK,scnsGyroInit(SCNS_GYRO_1));
#endif
    //scnsTimerMarkInited();
    //scnsBeepAdd("3");
    //scnsMenuAddFunction(0,test);
    //extern uint8 __readTest;
    //scnsMenuAdd(0,__readTest,,SCNS_TYPE_BOOLEAN,1,0xffff,0,0);
    //printfFL("Init finish %d error\n",errN);
    //scnsLcdDisplayInt16(SCNS_LCD_LAYER_AUTO,(Point){.x=errPlu.x,.y=errPlu.y+errN*scnsLcdFontSimSun16.fontHeight},errN,&scnsLcdFontSimSun16,SCNS_LCD_COLOR_YELLOW,SCNS_LCD_COLOR_BACKGROUND,1);
    //for(;scnsTimerGetUs()<displayUs;)
    //{
    //    scnsIwdgFeed();
    //    scnsMenuFresh();
    //}
    //scnsBeepAdd("4");
    //scnsMenuSetMode(SCNS_MENU_MODE_KEY|SCNS_MENU_MODE_SMCTP|SCNS_MENU_MODE_LCD);
#if defined(SCNS_TOUCH_SCREEN_ENABLE)&&SCNS_TOUCH_SCREEN_ENABLE==1
    scnsTouchScreenDisplayButtonAll(SCNS_LCD_LAYER_AUTO);
#endif
}